package com.wuhao.shootclient.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wuhao.shootclient.R;
import com.wuhao.shootclient.adapter.BlueDialogAdapter;
import com.wuhao.shootclient.bean.BluetoothDataVo;

import java.util.ArrayList;
import java.util.List;

/**
 * bledialog
 */
public class BlueListDialog extends Dialog {

    private TextView tvTitle;
    private RecyclerView recycDeviceId;
    private TextView tvMessage;
    private Button negtive;
    private View columnLine;
    private Button positive;
    private LinearLayout llOptions;
    private Button ckBtShutDown;
    private RotationLoadingView itemImage;

    //数据内容
    private String title;
    private String message;
    private String positiveSt,negtiveSt;
    private String shutDown;
    private int quantity = 1;
    private List<BluetoothDataVo> list;
    private List<Integer> integers;

    private BlueDialogAdapter adapter;

    private Context context;

    public BlueListDialog(@NonNull Context context) {
        super(context, R.style.BlueListDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_blue_list);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);


        //初始化界面控件
        initView();
        //初始化界面数据
        refreshView();
        //初始化界面控件的事件
        initEvent();
    }

    private void initView() {
        tvTitle = findViewById(R.id.tv_title);
        recycDeviceId = findViewById(R.id.recyc_device_id);
        tvMessage = findViewById(R.id.tv_message);
        negtive = findViewById(R.id.negtive);
        columnLine = findViewById(R.id.column_line);
        positive = findViewById(R.id.positive);
        llOptions = findViewById(R.id.ll_options);
        ckBtShutDown = findViewById(R.id.ck_bt_shut_down);
//        itemImage = findViewById(R.id.item_image);
    }

    //显示转菊花
    public void showItem(){
//        itemImage.setVisibility(View.VISIBLE);
//        itemImage.startRotationAnimation();
    }

    //隐藏转菊花
    public void hideItem(){
//        if (itemImage.getVisibility() != View.GONE){
//            itemImage.stopRotationAnimation();
//            itemImage.setVisibility(View.GONE);
//        }
    }

    private void initEvent() {
        //双按钮
        negtive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickTwoListener != null) {
                    onClickTwoListener.onNegtiveClick();
                }
            }
        });
        //双按钮
        positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickTwoListener != null) {
                    onClickTwoListener.onPositiveClick();
                }
            }
        });
        //单按钮
        ckBtShutDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (itemImage.getVisibility() == View.GONE){
//                    if (onClickShotListener != null) {
                        onClickShotListener.onShotDownClick();
//                    }
//                }
            }
        });
    }

    private void refreshView() {
        //标题
        if (!TextUtils.isEmpty(title)){
            tvTitle.setText(title);
            tvTitle.setVisibility(View.VISIBLE);
        }
        else {
            tvTitle.setVisibility(View.GONE);
        }
        //message 主题文字
        if (!TextUtils.isEmpty(message)){
            tvMessage.setText(message);
            tvMessage.setVisibility(View.VISIBLE);
        }
        else {
            tvMessage.setVisibility(View.GONE);
        }
        //按钮1个
        if (quantity == 1){
            if (!TextUtils.isEmpty(shutDown)){
                ckBtShutDown.setText(shutDown);
            }
            else {
                ckBtShutDown.setText("确定");
            }
            llOptions.setVisibility(View.GONE);
            ckBtShutDown.setVisibility(View.VISIBLE);
        }
        //按钮2个
        else if (quantity == 2){
            if (!TextUtils.isEmpty(negtiveSt)){
                negtive.setText(negtiveSt);
            }
            else {
                negtive.setText("取消");
            }
            if (!TextUtils.isEmpty(positiveSt)){
                positive.setText(positiveSt);
            }
            else {
                positive.setText("确定");
            }
            llOptions.setVisibility(View.VISIBLE);
            ckBtShutDown.setVisibility(View.GONE);
        }
        //列表
        if (list!=null && list.size()>0){
            adapter = new BlueDialogAdapter(list);
            recycDeviceId.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL, false));
            recycDeviceId.setAdapter(adapter);
            //点击连接蓝牙设备
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (onClickItemListener!= null){
                        onClickItemListener.onItemDownClick(position);
                    }
                }
            });
        }
        else {
            list = new ArrayList<>();
            adapter = new BlueDialogAdapter(list);
            recycDeviceId.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL, false));
            recycDeviceId.setAdapter(adapter);
            //点击连接蓝牙设备
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                    if (itemImage.getVisibility() == View.GONE){
                        if (onClickItemListener!= null){
                            onClickItemListener.onItemDownClick(position);
                        }
//                    }

                }
            });
        }
        if (integers != null && integers.size()>0){

        }
    }

    public void upData(List<BluetoothDataVo> leDevices){
        if (leDevices != null && leDevices.size() != 0){
            adapter.setNewData(leDevices);
        }

    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }

    public interface OnClickTwoListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick();

        /**
         * 点击取消按钮事件
         */
        public void onNegtiveClick();
    }

    public OnClickTwoListener onClickTwoListener;

    public BlueListDialog setOnClickTwoListener(OnClickTwoListener onClickTwoListener) {
        this.onClickTwoListener = onClickTwoListener;
        return this;
    }

    public interface OnClickShotListener {
        /**
         * 单按钮
         */
        public void onShotDownClick();
    }

    public OnClickShotListener onClickShotListener;

    public BlueListDialog setOnClickShotListener(OnClickShotListener onClickShotListener) {
        this.onClickShotListener = onClickShotListener;
        return this;
    }

    public interface OnClickItemListener {
        /**
         * 列表点击
         */
        public void onItemDownClick(int position);
    }

    public OnClickItemListener onClickItemListener;

    public BlueListDialog setItemOnClickListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPositiveSt() {
        return positiveSt;
    }

    public void setPositiveSt(String positiveSt) {
        this.positiveSt = positiveSt;
    }

    public String getNegtiveSt() {
        return negtiveSt;
    }

    public void setNegtiveSt(String negtiveSt) {
        this.negtiveSt = negtiveSt;
    }

    public String getShutDown() {
        return shutDown;
    }

    public void setShutDown(String shutDown) {
        this.shutDown = shutDown;
    }

    public List<BluetoothDataVo> getList() {
        return list;
    }

    public void setList(List<BluetoothDataVo> list) {
        this.list = list;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public List<Integer> getIntegers() {
        return integers;
    }

    public void setIntegers(List<Integer> integers) {
        this.integers = integers;
    }
}
